<?php
namespace app\admin\controller;

use app\common\component\admin\AdminService;
use app\common\component\admin\AdminValidate;

class Auth extends ControllerBase {
	public function login() {
		if($this -> request -> isPost()) {
			$post = $this -> request -> post();

			$resultAdminValidate = (new AdminValidate) -> login($post);
			if($resultAdminValidate['code'] === 0) {
				$resultLogin = (new AdminService) -> login($post);
				if($resultLogin['code'] === 0) {
					$this -> redirect('admin/Index/index');
				} else {
					echo '<meta charset="utf-8"><script>alert("' . $resultLogin['message'] . '!");</script>';
				}
			} else {
				echo '<meta charset="utf-8"><script>alert("' . $resultAdminValidate['message'] . '!");</script>';
			}
		}

		return $this -> view -> fetch();
	}

	public function logout() {
		(new AdminService) -> clearLoginSession();
		$this -> redirect('admin/Auth/login');
	}
}